package com.sto.stoapp.activity;

import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;

import com.sto.stoapp.StoApplication;
import com.sto.stoapp.db.StoDataBaseOperation;
import com.sto.stoapp.db.StoFilePreference;
import com.sto.stoapp.entity.DataBusinessEntity;
import com.sto.stoapp.status.EnumTableNames;
import com.sto.stoapp.util.DateUtil;
import com.sto.stoapp.util.Helper;
import com.sto.stoapp.widget.BaseActivity;
import com.sto.stoapp.widget.StoHhmEditText;

public class StoSetTimeActivity extends BaseActivity implements OnClickListener{
    Button start_day, start_time, save_time, cancel;
    StoHhmEditText set_date, set_time;
    String _NowDay="0";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.sto_set_time_dialog);
        
        initWidget();
        initListener();
    }
    
    private void initWidget() {
        // TODO Auto-generated method stub
        start_day = (Button)findViewById(R.id.start_day);
        start_time = (Button)findViewById(R.id.start_time);
        save_time = (Button)findViewById(R.id.save_time);
        cancel = (Button)findViewById(R.id.cancel);
        set_date = (StoHhmEditText)findViewById(R.id.set_date);
        set_time = (StoHhmEditText)findViewById(R.id.set_time);
        _NowDay = DateUtil.getTodayDate("-");
        set_date.setText(_NowDay);
        
        set_time.setText(DateUtil.getNowTime(":"));
    }
    
    private void initListener(){
        start_day.setOnClickListener(this);
        start_time.setOnClickListener(this);
        save_time.setOnClickListener(this);
        cancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.start_day:
            dateSetDialog(R.id.start_day);
            break;
        case R.id.start_time:
            timeSetDialog(R.id.start_time);
            break;
        case R.id.save_time:
            saveData();
            break;
        case R.id.cancel:
            this.finish();
            break;
        default:
            break;
        }
    }
    
    private void saveData() {
        long startDate = Long.parseLong(set_date.getText().toString()
                .replace("-", ""));
        
        long minDate = Long.parseLong(_NowDay.replace("-", ""));
        String afterTime = set_date.getText().toString() + " " + set_time.getText().toString()+":00";
        String beforeTime = DateUtil.getDateTime();
        
        if(startDate >= minDate){
            // 保存数据
            setTime(afterTime);
           if(addPieceRecord(beforeTime, afterTime)){
               Helper.showSoundToast(this, "修改时间成功", true);
               this.finish();
           }else{
               Helper.showSoundToast(this,"修改失败,请重新修改!", false);
           }
        }else{
            _NowDay = DateUtil.getTodayDate("-");
            set_date.setText(_NowDay);
            Helper.showSoundToast(this, "日期不能向前修改", false);
            
        }
        
    }
    
    
 // 年月日时分秒
    private void setTime(String systemTime) {
        if(!("").equals(systemTime)){
            StoApplication.getStoJni().SetSystemTime(DateUtil.getLongTime(
                    DateUtil.DEFAULT_DATE_TIME_FORMAT, systemTime)+"");
        }
    }
    
 // 增加一条记录
    private boolean addPieceRecord(String beforeTime, String afterTime) {
        boolean ret;
        DataBusinessEntity dataBusiness = new DataBusinessEntity();
        dataBusiness.initInsertData();
        
        // 扫描类型
        dataBusiness.setCommand_type(EnumTableNames.STATUS_TABLE_MODIFY_TIME_SYN.getOpCode());
        // 修改前时间
        dataBusiness.setRecv_person(beforeTime);
        
        // 修改后时间
        dataBusiness.setSend_person(afterTime);
        
        // 作为唯一修改上传状态的标识
        dataBusiness.setBar_code_number(DateUtil.getLongDateTime());
        
        // 网点编号
        dataBusiness.setNet_code(StoFilePreference.getInstance(this)
                .getSiteCode());
        // 工号
        dataBusiness.setJob_number(StoFilePreference.getInstance(this)
                .getUserCode());
        
        // 插入数据
        ret = StoDataBaseOperation.getInstance(this).insert(dataBusiness,
                EnumTableNames.STATUS_TABLE_MODIFY_TIME_SYN);
        
        
        return ret;
    }
    
    
    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
//        super.onBackPressed();
    }

    // 获取日期的Dialog
    private void dateSetDialog(final int id) {
        final DatePicker datePicker = new DatePicker(this);
        int year = datePicker.getYear();
        int month = datePicker.getMonth();
        int dayOfMonth = datePicker.getDayOfMonth();

        OnDateSetListener callBack = new OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {

                int month = monthOfYear + 1;
                String strMonth = null;
                String strDayOfMonth = null;
                if (0 < month && month < 10) {
                    strMonth = "0" + month;
                } else {
                    strMonth = "" + month;
                }
                if (0 < dayOfMonth && dayOfMonth < 10) {
                    strDayOfMonth = "0" + dayOfMonth;
                } else {
                    strDayOfMonth = "" + dayOfMonth;
                }
                String date = new StringBuilder().append(year).append("-")
                        .append(strMonth).append("-").append(strDayOfMonth)
                        .toString();
                if (id == R.id.start_day) {
                    set_date.setText(date);

                }
            }
        };
        DatePickerDialog datePickerDialog = new DatePickerDialog(this,
                callBack, year, month, dayOfMonth);

        datePickerDialog.show();
    }
    
    private void timeSetDialog(final int id) {
        final TimePicker timePicker = new TimePicker(this);
        int hour = timePicker.getCurrentHour();
        int minute = timePicker.getCurrentMinute();
        OnTimeSetListener timeListener = new OnTimeSetListener() {

            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                String min = "" + minute;
                if (minute < 10)
                    min = "0" + min;

                String time = hourOfDay + ":" + min;

                if (id == R.id.start_time) {
                    set_time.setText(time);
                }

            }
        };
        TimePickerDialog timeDialog = new TimePickerDialog(this, timeListener,
                hour, minute, true);
        timeDialog.show();
    }
    
}
